package com.hyt.it.ogt.kq.service.bm.den;

import com.alibaba.fastjson.JSONObject;
import com.hyt.it.ogt.kq.service.bm.feign.tps.TpsClient;
import com.hyt.model.tps.vo.MessageVo;

import cn.hutool.core.text.StrPool;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@RefreshScope
public class ShortMessage {
	
	@Autowired
	private TpsClient tpsClient;

	@Value("${tps.sms.officeId}")
	private String officeId;

	@Value("${tps.sms.appId}")
	private String appId;
	
	@Value("${tps.sms.appKey}")
	private String appKey;
	
	@Value("${tps.sms.sendType}")
	private String sendType;
	
	private String[] phonenumber;
	private String content;
	
	public ShortMessage(){
		
	}
	
	public ShortMessage(String[] phonenumber, String content){
		this.phonenumber = phonenumber;
		this.content = content;
	}
	
	public void setMessage(String[] phonenumber, String content){
		this.phonenumber = phonenumber;
		this.content = content;
	}
	
	public Integer sendMessage(String sendTypeTemp) {
		try {
			Boolean send = Boolean.FALSE;
			if (StringUtils.isNotEmpty(sendType)) {
				String[] sendTypes = sendType.split(StrPool.COMMA);
				for (int i = 0; i < sendTypes.length; i++) {
					if (sendTypeTemp.equals(sendTypes[i])) {
						send = Boolean.TRUE;
					}
				}
			}
			if (send) {
				MessageVo messageVo = new MessageVo();
				messageVo.setOfficeId(officeId);
				messageVo.setAppId(appId);
				messageVo.setAppKey(appKey);
				messageVo.setMobileNum(phonenumber);
				messageVo.setContent(content);
				String result = tpsClient.sendMessage(messageVo);
				JSONObject jsonObject = JSONObject.parseObject(result);
				boolean success = jsonObject.getBoolean("success");
				if (success) {
					System.out.println("发送短信成功:" + Arrays.toString(phonenumber) + " " + content);
					return 1;
				} else {
					return -2;
				}
			} else {
				System.out.println("发送短信失败send:" + Arrays.toString(phonenumber) + " " + content);
				return -2;
			}
		} catch (Exception e) {
			System.out.println("发送短信失败:" + e.toString());
			return -1;
		}
	}
}
